import { getTeams } from "admin/api/team";
import Vue from "vue";
import { schoolInfo } from "admin/settings";

const state = {
    teams: Vue.ls.get("teams") || [],
    currTeam: Vue.ls.get("curr") || { name: "请选择学校", id: "" },
};

const mutations = {
    SET_TEAMS: (state, teams) => {
        //注入logo
        state.teams = teams.map((item) => {
            return {
                ...item,
                ...schoolInfo.find((v) => item.id === v.id),
            };
        });
        Vue.ls.set("teams", state.teams);
    },
    REMOVE_TEAMS: (state, team) => {
        state.teams = [];
        Vue.ls.remove("teams");
    },
    SET_CURRENTEAM(state, team) {
        //修改logo和title
        this.commit("settings/SET_STATE", { logo: team.logo || "/static/admin/img/logo.png", title: team.schoolName || "智慧校园管理平台" });
        state.currTeam = team;
        Vue.ls.set("curr", state.currTeam);
    },
    REMOVE_CURRENTEAM: (state, team) => {
        state.currTeam = {};
        Vue.ls.remove("curr");
    },
};

const actions = {
    // 获取团队信息
    async getTeams({ commit, state }) {
        const response = await getTeams(state.token);
        const { data } = response;
        commit("SET_TEAMS", data);
        return data;
    },

    // 设置当前团队信息
    setCurrTeam({ commit, state }, team) {
        commit("SET_CURRENTEAM", team);
    },
};

export default {
    namespaced: true,
    state,
    mutations,
    actions,
};
